<?php
class Admin_login_Controller extends Template_Controller {
	
	public $template = 'admin_login/index';
	 
	public function __construct()
    {
        parent::__construct();
        $this->template->top = new View('admin/top');
        $this->template->bottom = new View('admin/bottom');
    }
    
   	public function __call($method, $arguments)
	{
		// Disable auto-rendering
		$this->auto_render = FALSE;
	}
	
	public function index()
	{
		$this->template->content = new View('admin_login/frm_login');
		if($this->session->get('error_msg'))
		{
			$mr['error_frm'] = $this->session->get('error_msg');
			$this->template->content->mr = $mr;
		}
	}
	
	private function _valid_login()
	{
		$login = $this->input->xss_clean($this->input->post('txt_username'));
		$pass = md5($this->input->post('txt_pass'));
		
		$result = ORM::factory('administrator')->where(array('admin_name'=>$login, 'admin_pass'=>$pass,'admin_status'=>1))->find();
		
		if($result->count_all())
			return $result->admin_id;
		else		
			return false;		
	}
	
	public function sm()
	{
		$valid = $this->_valid_login();
		if ($valid)	// if login access
		{
			$result = ORM::factory('administrator',$valid);
			$sess_user['username'] = $result->admin_name;
			$sess_user['id'] = $valid;
			$sess_user['name'] = $result->admin_name;
			$sess_user['email'] = $result->admin_email;
			
			$this->login_model->set_login(2,$sess_user);
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_login_success'));
			
			url::redirect('admin');
			die();
		}
		else
		{
			$this->session->set_flash('error_msg',Kohana::lang('errormsg_lang.error_user_pass'));
			url::redirect('admin_login');
			die();
		}		
	}
	
	function forget_pass()
	{
		$this->template->content = new View('admin_login/frmsetpass');		
		if($this->session->get('error_msg'))
		{
			$mr['error_frm'] = $this->session->get('error_msg');
			$this->template->content->mr = $mr;
		}
		if($this->session->get('success_msg'))
		{
			$mr['success_frm'] = $this->session->get('success_msg');
			$this->template->content->mr = $mr;
		}
	}
	
	private function _valid_forgotPass()
	{
		$email = $this->input->post('txt_email');	
		$result = ORM::factory('administrator')->where('admin_email',$email)->find();
		if($result->count_all())
			return $result->admin_id;
		else		
			return false;	
	}
	
	private function send_email($result)
    {
    	//Use connect() method to load Swiftmailer
		$swift = email::connect();
		 
		//From, subject
		//$from = array($this->site['site_email'], 'Yesnotebook get password');
		$str_random = rand(1000, 9999);
		$from = $this->site['site_email'];
		$subject = 'Forgot your password '.$this->site['site_name'];
		//HTML message
		$path = 'application/views/email_tpl/forgotpass.tpl';
		$fi = fopen($path,'r+');
		$html_content = file_get_contents($path); 	
		//Replate content	
		$name =$result->admin_name;		
		$html_content = str_replace('#first_name#',$name,$html_content);		
		$html_content = str_replace('#username#',$result->admin_name,$html_content);		
		$html_content = str_replace('#sitename#',$this->site['site_name'],$html_content);		
		$html_content = str_replace('#password#',$str_random,$html_content);
		//fwrite($fi, $html_content);
		fclose($fi);

		//Build recipient lists
		$recipients = new Swift_RecipientList;
		$recipients->addTo($result->admin_email);
		 
		//Build the HTML message
		$message = new Swift_Message($subject, $html_content, "text/html");
		 
		if($swift->send($message, $recipients, $from)){
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_thanks_post'));
			
			$this->db->update('admin',array('admin_pass' => md5($str_random)),array('admin_email' => $result->admin_email));
			
			url::redirect('admin_login/forget_pass');
			die();
		}
		
		 else {
			
		}		 
		// Disconnect
		$swift->disconnect();
    }
	
	private function _get_record()
    {
    	$form = array
	    (
	       'txt_email' => ''	        
	    );
		
		$errors = $form;
		
		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_email','required','email');
			if($post->validate())
 			{
 				$form = arr::overwrite($form, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('forgetpass_validation'));
				$str_error = '';
				foreach($errors as $id => $name) if($name) $str_error.='<br>'.$name;
				$this->session->set_flash('error_msg',$str_error);
				url::redirect('admin_login/forget_pass');
				die();
			}
        }
    }
    
	public function forgetpass_sm()
    {
		$record = $this->_get_record();
		$valid = $this->_valid_forgotPass();
		if ($valid)	// if login access
		{
			$result = ORM::factory('administrator',$valid);
			$this->send_email($result);
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_thanks_post'));
			url::redirect('admin_login');
			die();			
		} else {
			$this->session->set_flash('error_msg',Kohana::lang('forgetpass_lang.lbl_email_exit'));
			url::redirect('admin_login/forget_pass');
			die();
		}
	}
	
	public function out()
	{
		$this->login_model->unset_login(2);
		
		url::redirect('admin');		
		die();
	}
}
?>